import express from 'express'
import multer from 'multer'
import fs from 'fs'
import path from 'path'
import cors from 'cors'

const app = express()

app.use(cors())
app.use(express.json())

app.post('/download',(req,res) => {
  const filename = req.body.filename
  const filepath = path.join(process.cwd(),'static',filename)
  const content = fs.readFileSync(filepath)
  // 两个响应头
  res.setHeader('content-type','application/octet-stream')
  // 默认inline 在网页中打开图片 默认是预览不是下载
  // attachment 当作附件去下载
  res.setHeader('content-disposition',`attachment;filename=${filename}`)
  res.send(content)

})

app.listen(3000,() => {
  console.log('服务启动')
})